<template>
  <div class="detail-page">
    <van-loading class="loading" v-if="loading" />
    <div class="detail-title" v-html="news.title"></div>

    <div class="detail-desc">
      <span class="detail-source">{{news.source}}</span>
      <span>{{news.published_time}}</span>
    
      <div v-if="is_deregulation">
        <!--信批合规减分项 关联方风险提示 其他风险提示 同行监管违规项这四类的详情页-->
        <div class="deregulation-title deregulation-top">处罚类型</div>
        <div class="deregulation-content" v-html="news.AdminType"></div>
        <div class="deregulation-title violation_mar_bottom">处罚原因</div>
        <div class="deregulation-content" v-html="news.reason"></div>
        <div class="deregulation-title violation_mar_bottom">处罚内容</div>
        <div class="deregulation-content" v-html="fixContent"></div>

        <div v-if="attach_data.length>0" class="detail-attach">
          <div v-for="item in attach_data" class="attach_box">
            <div class="ahref" v-if="item.url.match(/\.pdf/gi)" @click="gotoPDF(BASIC_PDF + item.url, item.title)">{{ item.title }}</div>
            <div class="ahref" v-else @click="gotoPDF(BASIC_PDF + item.url, item.title)">{{ item.title }}</div>
          </div>
        </div>
      </div>

      <div v-else>
        <div v-if="news.content !==''" class="deregulation-content" v-html="fixContent"></div>
        <div v-if="api_type_sourceLink" class="original-link" @click="gotoOriginalLink(news.url)">原文链接</div>
        <div v-if="attach_data.length>0" class="detail-attach">
          <div v-for="item in attach_data" class="attach_box">
            <!-- <div class="ahref" v-if="item.url.match(/\.pdf/gi)" @click="gotoPDF(base_pdf_http + item.url, item.title)">{{ item.title }}</div> -->
            <div class="ahref" @click="gotoPDF(BASIC_PDF + item.url, item.title)">{{ item.title }}</div>
            <!-- <a v-if="item.url.match(/\.pdf/gi)" :href="`${base_pdf_http}${item.url}`" target="_blank">{{ item.title }}</a>
            <a :href="'https://view.officeapps.live.com/op/view.aspx?src='+encodeURIComponent(`${base_pdf_http}${item.url}`)" v-else target="_blank">{{ item.title }}</a> -->
          </div>
        </div>
      </div>

    </div>
    
  </div>
</template>

<script>
import { getNewsDetail } from '@/api/api';
import { BASIC_PDF } from '@/core/config';
import IngotCookie from '@/utils/cookie.js';
import getUrlStr from '@/utils/getParam.js';
import { Loading } from 'vant';
export default {
  name: 'NewsDetail',
  data () {
    return {
      // base_pdf_http:'https://gdpage.cfbond.com/pdf/',

      // 是否为信批合规减分项 关联方风险提示 其他风险提示 同行监管违规项这四类的详情页
      is_deregulation: getUrlStr('api_type')==='deregulation' || 'violation',
      api_type_bool: getUrlStr('api_type')==='violation', // || 'regulatory'
      api_type_sourceLink: getUrlStr('api_type')==='company' || 'opinion',
      loading: true,
      news: {
        id: '',
        title: '',
        published_time: '',
        content: '',
        source: '',
        url: '',
        attach_data:[],
        is_collection: false
      },
      fixContent:'',
      attach_data:[]
    };
  },
  created() {
    this.$store.commit('set_top_title','资讯详情');
  },
  mounted () {
    console.log("id",getUrlStr('id'));
    // this.fixContent = this.fixHTML(this.news.content);
    // console.log("fixContent",this.fixContent)
    // this.fixHTML(this.news.content);
    this.is_deregulation = getUrlStr('api_type') === 'deregulation' || getUrlStr('api_type') === 'violation';
    this.api_type_sourceLink = getUrlStr('api_type') === 'company' || getUrlStr('api_type') === 'opinion',
    console.log("api_type",this.api_type_sourceLink)

    getNewsDetail({
      api_type: getUrlStr('api_type'), // || 'opinion'  || 'company'
      id: getUrlStr('id'), // || '31555892270975395245551' || 'b06fa16530c0ab0c804aa627124f4a89'
      user_id: getUrlStr('user_id'),
    }).then((res) => {
      this.loading = false;
      this.news = res.data;
      console.log("数据:",this.news)
      // console.log("=======",typeof(this.news.attach_href)==='string')
      this.fixContent = this.fixHTML(this.news.content);

      if(this.news.attach_data && this.news.attach_data.length >0){
        this.attach_data = this.news.attach_data;
      }

      // if(getUrlStr('api_type')==="regulatory"){
      //   this.news.source = res.data.source_name;
      //   this.news.published_time = res.data.ptime;
      // }
      // if(this.news.attach_href && typeof(this.news.attach_href)=='object'){
      //   for(let i = 0;i<this.news.attach_href.length;i++){
      //     this.attach_data.push({title:this.news.attach_title[i],url:this.news.attach_href[i]})
      //   }
      // }else if(typeof(this.news.attach_href)==='string'){
      //   this.attach_data = [{title:this.news.attach_title,url:this.news.attach_href}]
      // };
    });
  },
  methods:{
    gotoOriginalLink(url){
      if(getUrlStr('source')==='ios'){
        let channelUrl=decodeURIComponent(url);
        window.webkit.messageHandlers.NoticeOriginalLink.postMessage(channelUrl);
      }else if(getUrlStr('source')==='android'){
        window.AndroidWebView.NoticeOriginalLink(url);
      }
    },
    gotoPDF(url,title) {
      console.log("url === ",url)
      console.log("title === ",title)
      if(getUrlStr('source')==='ios'){
        // console.log(IngotCookie.get('source'))
        window.webkit.messageHandlers.NoticeFileView.postMessage(title+'_'+url);
      }else if(getUrlStr('source')==='android'){
        // console.log(IngotCookie.get('source'))
        window.AndroidWebView.NoticeFileView("true", url, title);
      }
    },
    fixHTML(str){
      // console.log("string",str)
      var _str;
      if(typeof str != 'string'){
        _str = str; 
      }else{
        if(str.indexOf('</br>') != -1){
          console.log("文章含有换行")
          _str = "<div class='all-content'><style>.all-content p{text-indent: 2em;}</style><p>"+str.replace(/<\s*[\/]?br>/gi,'</p><p>')+"</div></div>";
          _str = _str.replace(/\u00a0/gi,'</p><p>')
        }else if(str.indexOf('<img') != -1){
          console.log("文章含有图片标签")
          _str = str.replace(/<img/gi,"<img style='display:block;width:100%;text-indent: 0em'")
        }else {
          _str = str;
        }
      }
      // console.log("_str",_str)
      return _str;
    }
  }
};
</script>
<style>

.loading{
  position: absolute;
  top: calc(50vh - 24px);
  left:calc(50% - 24px);
}
.detail-page {
  text-align: left;
  padding: 19px;
}
.detail-title {
  font-size: 19px;
  color: #222222;
  font-weight: bold;
}
.detail-desc {
  font-size: 12px;
  color: #bcbcbc;
  padding: 8px 0;
}
.detail-source {
  padding-right: 17px;
}
.detail-content {
  color: #222222;
  font-size: 16px;
  line-height: 24px;
}
.detail-attach{
  color: #222222;
  font-size: 16px;
  line-height: 24px;
  /* border-top: 1px solid #f1f1f1; */
}
.attach_box{
  padding: 5px 0;
  margin: 5px 0;
  /* border-radius: 3px;
  color: #C29854;
  border: 1px solid #E9DBC2;
  background-color: #FAF4E9; */
}
.ahref{
  font-size: 1em;
  color: #3681df;
  line-height: 1.6em;
}
.attach-title {
  font-weight: 600;
  color: #222;
}
.violation_content_title{
  font-weight: 600;
  color: #222;
  line-height: 2em;
}
.violation_mar_bottom {
  margin-top:10px;
}
.deregulation-top{
  padding-top:5px;
}
.deregulation-title{
  font-weight: bold;
  font-size:17px;
  color: #222;
  line-height: 29px;
}
.deregulation-content{
  font-size:16px;
  color: #222;
  line-height: 29px;
}
.deregulation-content p {
  text-indent: 2em;
  }
.all-content p {
  text-indent: 2em;
  }
.original-link {
  font-size: 1.2em;
  color: #007FE6;
  margin-top: 1em;
  text-decoration: underline;
}
</style>